$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'exam/equestionmanagement/list',
        datatype: "json",
        colModel: [			
        	/*{ label: '编号', name: 'id', index: 'id', width: 50, key: true },*/
			{ label: '试题内容', name: 'content', index: 'content', width: 80 ,align:'center' }, 			
			{ label: '章节', name: 'sectionName', index: 'section_id', width: 80 ,align:'center' }, 			
			{ label: '专业', name: 'marjorName', index: 'major_id', width: 80,align:'center'  }, 
			{ label: '试题等级', name: 'grade', index: 'grade', width: 80,align:'center' ,
				formatter:function(grade,index,row){
					if(grade==1){
						grade="简单";
					}else if(grade==2){
						grade="中等";
					}else{
						grade="极难";
					}
					return grade;
				}
			}, 	
			{ label: '类型', name: 'typeName', index: 'type_id', width: 80,align:'center'  }, 			
			/*{ label: '答案', name: 'optioncontent', index: 'answer_id', width: 80 }, */			
			{ label: '创建人', name: 'createUser', index: 'create_user', width: 80,align:'center' }, 			
			{ label: '创建时间', name: 'createTime', index: 'create_time', width: 80,align:'center', 
				formatter:function(createTime,index,row){
					return getDate(createTime);
				}	
			}/*, 
			{ label: '操作', name: 'button', index: '', width: 40 ,
				formatter:function(button,index,row){
					return button= "<div class=\"fa fa-file-text\" onclick='seleleShow(\""+ row.id + "\")'' style=\"background:url('') no-repeat;\"></div>"
				}
			}	*/
			/*{ label: '修改人', name: 'updateUser', index: 'update_user', width: 80 }			
			{ label: '修改时间', name: 'updateTime', index: 'update_time', width: 80} 			
			{ label: '创建部门', name: 'deptId', index: 'dept_id', width: 80 }			
			{ label: '创建人id', name: 'userId', index: 'user_id', width: 80 }, 			
			{ label: '是否停用  0正常  1删除', name: 'disFlag', index: 'dis_flag', width: 80 }*/			
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        },
    });
//    vm.getSectionList();
	vm.getMajorList();
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		allHide:false,
		showListTable:false,
		title: null,
		typeLists: {},
		eQuestionManagement: {},
		q:{
			majorId : null,
			sectionId : null
		  },
		majorLists : {},
		sectionLists :{},
		showAnswerOptions: false,
		answer: 'A',
		options: [
			{ value: '', ABC: 'A', placeholder: '选项A' },
			{ value: '', ABC: 'B', placeholder: '选项B' },
			{ value: '', ABC: 'C', placeholder: '选项C' },
			{ value: '', ABC: 'D', placeholder: '选项D' }
		]
	},
	methods: {
		query: function () {
			vm.reload();
		},
		reset : function(){
			vm.q.majorId = null;
			vm.q.sectionId = null;
			vm.query();
		},
		add: function(){
			vm.showList = false;
			vm.showListTable = true;
			vm.title = "新增";
			vm.eQuestionManagement = {};
			vm.getTypeList();
			vm.getMajorList();
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
			vm.showListTable = true;
            vm.title = "修改";
            vm.getInfo(id);
            vm.getTypeList();
			vm.getMajorList();
            vm.getOptionList(id);
		},
		imports: function () {
			vm.showList = false;
			vm.allHide = true;
            vm.title = "试题导入";
            vm.getSectionList();
			vm.getMajorList();
            
		},
		impo: function (event) {
			var formData = new FormData();
			formData.append('file', $('#file')[0].files[0]);
			formData.append('majorId',vm.eQuestionManagement.majorId);
			formData.append('sectionId',vm.eQuestionManagement.sectionId);
			$.ajax({
                url: baseURL + "exam/equestionmanagement/import",
                type: 'POST',
                cache: false,
                data: formData,
                processData: false,
                contentType: false,
                success: function (r) {
                	console.log(r);
                	var status =  r.status;
                	alert("您已成功插入"+status+"条信息");
                	$("#jqGrid").trigger("reloadGrid");
                },
                error: function (err) {
                }

			});
			
		},
		download:function(){
			location.href=baseURL+"exam/equestionmanagement/download";
		},
		saveOrUpdate: function (event) {
			var url = vm.eQuestionManagement.id == null ? "exam/equestionmanagement/save" : "exam/equestionmanagement/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(event),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "exam/equestionmanagement/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "exam/equestionmanagement/selectOne/"+id, function(r){
                vm.eQuestionManagement = r.data.questionManagementEntity;
                console.log(r.data)
            });
		},
		reload: function (event) {
			vm.showList = true;
			vm.allHide = false;
			vm.showListTable = false;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
				postData:{
					'majorId': vm.q.majorId,
					'sectionId': vm.q.sectionId
				},
                page:page
            }).trigger("reloadGrid");
		},
		//获取试题类型列表.
		getTypeList: function (event) {
			$.get(baseURL + "exam/questionstype/getList", function(r){
				vm.typeLists = r.typeList;
            });
		},
		//获取答案列表
		getOptionList:function(id){
			$.get(baseURL + "exam/equestionmanagement/listOption/"+id, function(r){
				vm.optionLists = r.list;
            });
		},
		//获取专业列表
		getMajorList:function(){
			$.get(baseURL + "exam/major/getList",function(r){
				vm.majorLists = r.majorList;
			});
		},
		//获取章节列表
		getSectionList:function(majorId,disFlag){
			$.get(baseURL + "exam/section/selectByMajorId/"+majorId+"/"+disFlag, function(r){
				vm.sectionLists = r.section;
            });
		},
		//获取章节列表
		getSectionLists:function(majorId,disFlag){
			$.get(baseURL + "exam/section/selectByMajorIds/"+majorId+"/"+disFlag, function(r){
				vm.sectionLists = r.section;
            });
		},
		selectChangeHandler: function () {

			this.showAnswerOptions = true;
		},
		selectChangeHandlerSection: function () {
			var majorId =this.eQuestionManagement.majorId;
			var flag = 0 ;
			vm.getSectionList(majorId,flag);
		},
		selectChangeHandlerSections: function () {
			var majorId =this.q.majorId;
			var flag = 0 ;
			vm.getSectionLists(majorId,flag);
		},
		getData: function () {
			vm.eQuestionManagement.answerId=this.answer;
			
			console.log(this.answer)
			
			var param= {
					option :this.options,
					question:vm.eQuestionManagement
			}
			vm.saveOrUpdate(param);
		},
		formatOptionData: function  () {
			// 格式化，设置其 A B C
			var arrABC = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'];
			this.options.forEach(function(item, index) {
				var ABC = arrABC[index].toUpperCase();
				item.ABC = ABC;
				item.placeholder = '选项' + ABC;
			})
		},
		addOption: function  (index) {
			console.log(index);
			this.options.splice(index + 1, 0, { value: '', placeholder: '选项' });
			this.formatOptionData();
		},
		delOption: function  (index) {
			this.options.splice( index, 1 );
			this.formatOptionData();
		},
		
		toggleContainer: function  () {
			var domArr = [
				{ groupId: 1, dom: $('.selection-container') },
				{ groupId: 2, dom: $('.selection-container') },
				{ groupId: 3, dom: $('.judge-container') },
			]
			var targetDomArr = domArr.filter(function(item){
				item.dom.addClass('hide');
				return item.groupId === parseInt(groupId);
			})
			if (!targetDomArr.length) return;
			targetDomArr[0].dom.removeClass('hide');
			initData();
		}
		
		
	} 
}); 

function seleleShow(id){
	$.ajax({
		url:baseURL + "exam/equestionmanagement/selectOne",
		dataType:"json",
	    data:{"id":id},
		type:"POST",
		success:function(obj){
			console.info(obj.list);
		}
		
	})
}



	// initData();
	function getDate(time){
		 var date = new Date(time);
	     var y = 1900 + date.getYear();
	     var m = "0" + (date.getMonth() + 1);
	     var d = "0" + date.getDate();
	     var h = date.getHours();
	     if (h < 10) {
	         h = "0" + h;
	     }
	     var min = date.getMinutes(); 
	     if (min < 10) {
	         min = "0" + min;
 	     }
	     var s = date.getSeconds();
	     if (s < 10) {
	         s = "0" + s;
	     }
	     return y + "年" + m.substring(m.length - 2, m.length) + "月" + d.substring(d.length - 2, d.length) + "日 " + h + ":" + min + ":" + s;
	}


